package com.qqtop10.manager.controller;

import com.qqtop10.manager.model.Address;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.model.Shop;
import com.qqtop10.manager.model.User;
import com.qqtop10.manager.service.AddressService;
import com.qqtop10.manager.service.ShopService;
import com.qqtop10.manager.service.UserService;
import com.qqtop10.manager.utils.RandomUtils;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping({"/manager/delivery/"})
@Controller
public class DeliveryController
  extends BaseController
{
  @Autowired
  @Qualifier("shopService")
  private ShopService shopService;
  @Autowired
  @Qualifier("addressService")
  private AddressService addressService;
  @Autowired
  @Qualifier("userService")
  private UserService userService;
  
  @RequestMapping({"all.do"})
  @ResponseBody
  public Result all(HttpServletRequest request, HttpServletResponse response, Shop input)
  {
    String where = request.getParameter("where");
    StringBuffer sb = new StringBuffer();
    try
    {
      String[] array = where.split(",");
      if (array.length > 0) {
        if (Integer.parseInt(array[0]) > 0) {
          for (int i = 0; i < array.length; i++)
          {
            sb.append("shop_type = " + array[i]);
            if (i < array.length - 1) {
              sb.append(" and ");
            }
          }
        } else if (Integer.parseInt(array[0]) < 0) {
          for (int i = 0; i < array.length; i++)
          {
            sb.append("shop_type != " + Math.abs(Integer.parseInt(array[i])));
            if (i < array.length - 1) {
              sb.append(" and ");
            }
          }
        }
      }
      PagingArgs pagingArgs = getPagingArgs();
      input.setPromise(sb.toString());
      return page(this.shopService.count2(input), this.shopService.all(input, pagingArgs));
    }
    catch (Exception e)
    {
      return excep(e);
    }
  }
  
  @RequestMapping({"approve.do"})
  @ResponseBody
  public Result approve(Shop input)
  {
    if ((input == null) || (input.getId() == null) || (input.getStatus() == null)) {
      return new Result(1, "有些必须的参数未指定。");
    }
    try
    {
      input.setMender(getLogindUuid());
      input.setUpdatetime(now());
      if (this.shopService.approve(input)) {
        return ok("操作成功!");
      }
      return err("操作失败!");
    }
    catch (Exception e)
    {
      return excep(e);
    }
  }
  
  @RequestMapping({"update.do"})
  @ResponseBody
  public Result update()
  {
    Shop input = new Shop();
    fill(input);
    MultipartFile logo = getFile("logo");
    MultipartFile businessLicence = getFile("business_licence");
    if ((logo != null) && (!logo.isEmpty())) {
      input.setLogo(save(logo));
    }
    if ((businessLicence != null) && (!businessLicence.isEmpty())) {
      input.setBusiness_licence(save(businessLicence));
    }
    if ((input == null) || (input.getCellphone() == null) || (input.getCity() == null) || (input.getContacts() == null) || 
      (input.getDetailed_address() == null) || (input.getDistrict() == null) || (input.getEmail() == null) || 
      (input.getIdcard_number() == null) || (input.getName() == null) || (input.getProvince() == null) || 
      (input.getTelephone() == null)) {
      return err("有些必须的参数未指定。");
    }
    try
    {
      input.setProvince_id(this.addressService.selectByFullName("中国," + input.getProvince()).getId());
      input.setCity_id(
        this.addressService.selectByFullName("中国," + input.getProvince() + "," + input.getCity()).getId());
      input.setDistrict_id(this.addressService
        .selectByFullName("中国," + input.getProvince() + "," + input.getCity() + "," + input.getDistrict())
        .getId());
      if ((input.getId() == null) || (input.getId().intValue() == 0))
      {
        int i = this.userService.phoneIsExist(input.getCellphone());
        if (i > 0) {
          return err("操作失败：该手机号码已存在");
        }
        User user = new User();
        user.setUuid(RandomUtils.makeUUID());
        user.setAccount(input.getCellphone());
        user.setNickname(user.getAccount());
        
        user.setPassword("0192023A7BBD73250516F069DF18B500");
        user.setCellphone(input.getCellphone());
        user.setSex(Integer.valueOf(1));
        user.setType(Integer.valueOf(1));
        user.setHead_sculpture("");
        user.setStatus(Integer.valueOf(0));
        user.setIsdeleted(Boolean.valueOf(false));
        user.setCreatetime(new Date());
        user.setCreator(super.getLogindUuid());
        
        input.setId(null);
        
        input.setUuid(RandomUtils.makeUUID());
        
        input.setUser_uuid(user.getUuid());
        
        input.setIndustry_category_uuid("");
        
        input.setPromise("");
        
        input.setShop_type(Integer.valueOf(3));
        
        input.setStatus(Integer.valueOf(1));
        
        input.setBank_account("");
        
        input.setBank_name("");
        
        input.setDescription("");
        
        input.setIdcard("");
        
        input.setLogo("");
        
        input.setBusiness_licence("");
        
        input.setIsdeleted(Boolean.valueOf(false));
        
        input.setCreator(super.getLogindUuid());
        
        input.setCreatetime(new Date());
        int ii = this.userService.insertSelective(user);
        if (ii > 0)
        {
          if (this.shopService.insertSelective(input) > 0) {
            return ok("操作成功!", input);
          }
          return err("操作失败!");
        }
        return err("操作失败!");
      }
      String name = input.getName();
      input.setName(null);
      input.setCellphone(null);
      input.setMender(getLogindUuid());
      input.setCellphone(null);
      if (this.shopService.updateBySelective(input) > 0)
      {
        input.setName(name);
        return ok("操作成功!", input);
      }
      return err("操作失败!");
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return excep(e);
    }
  }
}
